package cn.wapache.timed.i18n;

import cn.wapache.exception.Exceptions;
import cn.wapache.i18n.I18n;
import cn.wapache.timed.exception.I18nScheduleException;

/**
 * 定义了各种code和默认提示内容
 * @author ykuang
 *
 */
public class Si18n {

	// 0           1234                56     789
	private static String org_wapache = "0x0";
	private static String org_wapache_schedule = org_wapache + "0200";
	
	private static String org_wapache_schedule_common = org_wapache + org_wapache_schedule + "01";
	private static String org_wapache_schedule_job = org_wapache + org_wapache_schedule + "01";
	private static String org_wapache_schedule_trigger = org_wapache + org_wapache_schedule + "02";

	public static String enum_switch_miss_some_items = org_wapache_schedule_common + "001";
	public static String enum_switch_miss_some_items_message = "missing case statement when traversal enum type in a switch statement : %s ";
	
	public static String clone_object_failed = org_wapache_schedule_common + "001";
	public static String clone_object_failed_message = "object is not cloneable.";
	
	public static String argument_is_null_or_empty = org_wapache_schedule_common + "001";
	public static String argument_is_null_or_empty_message = "the argument %s[%s] cannot be null or empty.";

	public static String property_is_null = org_wapache_schedule_common + "001";
	public static String property_is_null_message = "the property %s[%s] cannot be null.";
	
	public static String property_is_immutable = org_wapache_schedule_common + "002";
	public static String property_is_immutable_message = "the %s property is immutable，cannot be modified.";
	
	public static String job_already_exists = org_wapache_schedule_job + "001";
	public static String job_already_exists_message = "Unable to store Job : '%s', because one already exists with this identification.";

	
	public static String job_group_or_name_is_null = org_wapache_schedule_job + "001";
	public static String job_group_or_name_is_null_message = "the group[%s] or name property[%s] is null.";

	public static String job_group_argument_is_null_or_empty = org_wapache_schedule_job + "002";
	public static String job_group_argument_is_null_or_empty_message = "Job group[%s] cannot be null or empty.";
	
	public static String job_name_argument_is_null_or_empty = org_wapache_schedule_job + "003";
	public static String job_name_argument_is_null_or_empty_message = "Job name[%s] cannot be null or empty.";
	
	public static String trigger_group_or_name_is_null = org_wapache_schedule_trigger + "001";
	public static String trigger_group_or_name_is_null_message = "the group[%s] or name property[%s] is null.";

	public static String trigger_misfire_instruction_is_invalid = org_wapache_schedule_trigger + "001";
	public static String trigger_misfire_instruction_is_invalid_message = "The misfire instruction code [%s] is invalid for this type of trigger.";
	
	static {
		initialize();
	}

	private static void initialize(){
		
		I18n.register(I18n.newService("wapache.schedule.service.i18n", " the wapache schedule framework's i18n service.").putAll(
				
			 I18n.newEntry(job_already_exists, job_already_exists_message)
			,I18n.newEntry(trigger_group_or_name_is_null, trigger_group_or_name_is_null_message)
			
		));
		
	}
	

	public static String validateArg(String argName, String currentValue, String newValue) {
		
		if (currentValue!=null) {
            throw Exceptions.illegalArgument(Si18n.property_is_immutable,"argName");
        }
        
        if (newValue == null || newValue.trim().length() == 0) {
            throw Exceptions.illegalArgument(Si18n.argument_is_null_or_empty, "argName", newValue);
        }
        
        return newValue.trim();
        
	}
	
	public static void validateProperties(Object...properties) {
		for (int i = 0; i < properties.length; i=i+2) {
			if (properties[i+1]!=null) {
				throw Exceptions.illegalArgument(Si18n.property_is_null,properties[i]);
			}
		}
	}
	
	public static I18nScheduleException newException(String code, Object...values){
		return new I18nScheduleException(code,values);
	}
	
}
